package com.three.iot.rest.controller.api;

import com.three.iot.api.UserDeviceService;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.rest.controller.common.AbstractDeviceAttrController;
import com.three.iot.rest.utils.RequestUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author luiz
 * @Title: UserDeviceController
 * @ProjectName iot-server
 * @Description: 获取用户所有设备列表
 * @date 2019-05-13 16:25
 */
@RestController
@RequestMapping("/api")
@Api(tags = "用户设备相关", description = "用户设备相关API")
@Slf4j
public class UserDeviceController  extends AbstractDeviceAttrController {
    private static final String LOG_HEAD="【用户设备】";
    @Resource
    UserDeviceService userDeviceService;

    /**
     * 获取设备列表
     */
    @ApiOperation("获取设备列表")
    @GetMapping("/findDeviceList")
    private Result findDeviceList(HttpServletRequest request) {
        try{
            String userNo= RequestUtils.getCurrentUserNo(request);
            log.info(LOG_HEAD+"获取设备列表:{}",userNo);
            return userDeviceService.findDeviceList(userNo);
        }catch (Exception e){
            log.error(LOG_HEAD+"获取设备列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    /**
     * 按照设备编号、所属组获取属性数据详情
     */
    @ApiOperation("按照设备编号、所属组获取属性数据详情")
    @GetMapping("/findMultiAttrDataList/{iotId}/{groupNo}")
    private Result findMultiAttrDataList(@PathVariable("iotId") String iotId,@PathVariable("groupNo")String groupNo, HttpServletRequest request) {
        try{
            String userNo= RequestUtils.getCurrentUserNo(request);
            log.info(LOG_HEAD+"获取设备数据详情,userNo:{},iotId:{}groupNo:{}",userNo,iotId,groupNo);
            return userDeviceService.findMultiAttrDataList(iotId,groupNo,userNo);
        }catch (Exception e){
            log.error(LOG_HEAD+"获取设备数据详情发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }





}
